package imp.core.basics.date;

import imp.core.basics.ErrorHandler;


/**
 * The class for representing a date.
 * @author Patrick Arnold
 * @since 08.09.2010
 *
 */
public class Date {

	/** The day of the date. */
	private int day;
	
	/** The month of the date. */
	private int month;
	
	/** The year of the date. */
	private int year;
	
	/** The time of the date (optional). */
	private Time time;
	
	
	/**
	 * Constructor - Creates a new date object.
	 * @param day The day.
	 * @param month The month.
	 * @param year The year.
	 */
	public Date( int day, int month, int year) {
		this.day = day;
		this.month = month;
		this.year = year;
	}
	
	
	/**
	 * Constructor - Creates a new date object including a time.
	 * @param day The day.
	 * @param month The month.
	 * @param year The year.
	 * @param hour The hour of the time.
	 * @param minute The minute of the time.
	 */
	public Date( int day, int month, int year, int hour, int minute) {
		this.day = day;
		this.month = month;
		this.year = year;
		this.time = new Time( hour, minute);
	}
	
	
	/**
	 * Constructor - Creates a new date object including a time.
	 * @param day The day.
	 * @param month The month.
	 * @param year The year.
	 * @param hour The hour of the time.
	 * @param minute The minute of the time.
	 * @param second The second of the time.
	 */
	public Date( int day, int month, int year, int hour, int minute, int second) {
		this.day = day;
		this.month = month;
		this.year = year;
		this.time = new Time( hour, minute);
	}
	
	
	
	/**
	 * Constructor - Creates a new date object either out of a date string in the form DD.MM.YYYY
	 * or of a date string in the form DD.MM.YYYY, HH:MM:SS.
	 * @param dateString The date string.
	 * @param time Specifies whether the date string has a time specification (DD.MM.YYYY, HH:MM:SS)
	 * or not (DD.MM.YYYY).
	 */
	public Date( String dateString, boolean time) {
		
		if( dateString == null) {
			this.day = 0;
			this.month = 0;
			this.year = 0;
			return;
		}
		
		dateString = dateString.trim();
		
		try {
		
    		String str_day = dateString.substring( 0, 2);
    		String str_month = dateString.substring( 3, 5);
    		String str_year = dateString.substring( 6, 10);
    		
    		this.day = Integer.parseInt( str_day);
    		this.month = Integer.parseInt( str_month);
    		this.year = Integer.parseInt( str_year);
    		
    		
    		if( time) {
    			
    			String str_hour = dateString.substring( 12, 14); 
    			String str_minute = dateString.substring( 15, 17); 
    			String str_second = dateString.substring( 18, 20); 
    			
    			int hour = Integer.parseInt( str_hour);
    			int minute = Integer.parseInt( str_minute);
    			int second = Integer.parseInt( str_second);
    			
    			this.time = new Time( hour, minute, second);
    			
    		}
		
		} catch( Exception e) {
			
			String message = "Date string could not be generated.";
			String location = ErrorHandler.getParserLocation();
			ErrorHandler.error( message, location, e);
			
			this.day = 0;
			this.month = 0;
			this.year = 0;
			
		}
		
	}
	
	

	/**
	 * Returns the day of the Date.java.
	 * @return The day.
	 */
	public int getDay() {
		return day;
	}



	/**
	 * Returns the month of the Date.java.
	 * @return The month.
	 */
	public int getMonth() {
		return month;
	}



	/**
	 * Returns the year of the Date.java.
	 * @return The year.
	 */
	public int getYear() {
		return year;
	}
	
	
	
	/**
	 * Returns the date in the form DD.MM.YYYY.
	 * @return The date in the form DD.MM.YYYY.
	 */
	public String getDate() {
		
		String day_str = "" + day;
		if( day < 10) {
			day_str = "0" + day_str;
		}
		
		String month_str = "" + month;
		if( month < 10) {
			month_str = "0" + month_str;
		}
		
		String date = day_str + "." + month_str + "." + year;
		return date;
		
	}
	
	
	
	/**
	 * Returns the date in the form of DD.MM.YYYY, HH:MM:SS.
	 * @return The date in the form of DD.MM.YYYY, HH:MM:SS
	 */
	public String getDateAndTime() {
		String re = getDate() + ", " + time.getTime();
		return re;
	}
	
	
	/**
	 * Returns the time of the date.
	 * @return The time of the date (null if there is no date specified). 
	 */
	public Time getTime() {
		return time;
	}
	
	
	/**
	 * Returns a unique and monotonous increasing number of a date.
	 * @return A unique and monotonous increasing number of a date.
	 */
	public int getDateKey() {
		
		int key = (year * 1000) + (month * 40) + day;
		return key;
		
	}
	
	
}